<?php 
defined('IN_IXDCW') or exit('Access Denied');
class FriendLinkModule extends BasicModule {
	private $_itemid;
	private $_table;
	private $_fields;

	function __construct() {
		parent::__construct();
		global $db;
		$this->_table = $db->getPre().'link';
		$this->_fields = array('typeid','areaid','level','title','style','thumb','introduce','addtime','editor','edittime','template', 'status', 'linkurl');
    }

	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	function pass($post) {
		global $L;
		if(!is_array($post)) return false;
		if(!$post['typeid']) return $this->err($L['link_pass_type']);
		if(!$post['title']) return $this->err($L['link_pass_site']);
		if(!$post['linkurl']) return $this->err($L['link_pass_url']);
		return true;
	}

	function set($post) {
		global $MOD, $DT_TIME, $_username, $_userid;
		if(!$this->_itemid) $post['addtime'] = $DT_TIME;
		$post['edittime'] = $DT_TIME;
		$post['editor'] = $_username;
		clear_upload($post['thumb']);
		return $post;
	}

	function getOne() {
        return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid='$this->_itemid'");
	}

	function getList($condition = '1', $order = 'listorder DESC, itemid DESC') {
		global $MOD,$items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql);
		while($r = $this->_db->fetchArray($result)) {
			$r['title'] = set_style($r['title'], $r['style']);
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			$r['typename'] = $TYPE[$r['typeid']]['typename'];
			$r['typeurl'] = $MOD['link_url'].rewrite('index.php?typeid='.$r['typeid']);
			$lists[] = $r;
		}
		return $lists;
	}

	function add($post) {
		global $DT, $MOD, $module;
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$this->_itemid = $this->_db->insertId();
		return $this->_itemid;
	}

	function edit($post) {
		global $DT, $MOD, $module;
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table, $post, "itemid={$this->_itemid}", '', $this->_fields));
		return true;
	}

	function delete($itemid, $all = true) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { 
				$this->delete($v, $all); 
			}
		} else {
			$this->_itemid = $itemid;
			$r = $this->getOne();
			if($all) {
				$userid = get_user($r['editor']);
				if($r['thumb']) PictureUpload::deletePicture($r['thumb'], $userid);
				$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
			}
		}
	}

	function check($itemid) {
		global $_username, $DT_TIME;
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->check($v); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=3,editor='$_username',edittime=$DT_TIME","itemid=$itemid"));
			return true;
		}
	}

	function order($listorder) {
		if(!is_array($listorder)) return false;
		foreach($listorder as $k=>$v) {
			$k = intval($k);
			$v = intval($v);
			$this->_db->query(Sql::update($this->_table,"listorder=$v","itemid=$k"));
		}
		return true;
	}

	function level($itemid, $level) {
		$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
		$this->_db->query(Sql::update($this->_table,"level=$level","itemid IN ($itemids)"));
	}

}
?>